package com.jin.webvideo.util.sys;

import java.io.File;
import java.util.Locale;

public class SystemUtil {
    private final static String[] agent = { "android", "iphone", "ipod","ipad", "windows phone", "mqqbrowser", "harmonyos" }; //定义移动端请求的所有可能类型

    public static boolean isWin(){
        return osName().contains("windows");
    }
    public static boolean isLinux(){
        return osName().contains("linux");
    }
    public static String osName(){
        return System.getProperty("os.name").toLowerCase(Locale.ROOT);
    }
    public static long fileSize(String file) {
        return fileSize(new File(file));
    }
    public static long fileSize(File file) {
        if (file.isFile()) {
            return file.length();
        }
        long filesSize = 0L;
        File[] files = file.listFiles();
        if (files == null) {
            return filesSize;
        }
        for (File fileItem : files) {
            filesSize = filesSize + fileSize(fileItem);
        }
        return filesSize;
    }
    public static int deleteFile(File file){
        int count = 0;
        if (file.isDirectory()){
            File[] chldFiles = file.listFiles();
            for (File item : chldFiles){
                count += deleteFile(item);
            }
        }
        count = count + (file.delete()?1:0);
        return count;
    }
    /**
     * 判断User-Agent 是不是来自于手机
     * @param ua
     * @return
     */
    public static boolean isMobile(String ua) {
        ua = ua.toLowerCase(Locale.ROOT);
        boolean flag = false;
        if (!ua.contains("Windows NT") || (ua.contains("Windows NT") && ua.contains("compatible; MSIE 9.0;"))) {
            // 排除 苹果桌面系统
            if (!ua.contains("Windows NT") && !ua.contains("Macintosh")) {
                for (String item : agent) {
                    if (ua.contains(item)) {
                        flag = true;
                        break;
                    }
                }
            }
        }
        return flag;
    }

}
